//
// Created by root on 19-8-7.
//

#include "AndHook.h"
#include "hook_app_create.h"
#include "log_utils.h"
#include "inject_compacter.h"
#include "dex_utils.h"

#define TAG "AppOnCreate"

static jmethodID old_method= nullptr;

//public void callApplicationOnCreate(Application app)
void my_call_app_oncreate(JNIEnv *env,jobject instance,jobject app){
    if(!old_method){
        LOGE("old_method is nullptr\n");
        return;
    }
    inject_compacter(env);
    std::string pkg_name_str=get_package_name(env,app);
    LOGD("before callApplicationOnCreate of app:%s\n",pkg_name_str.c_str());
    env->CallVoidMethod(instance,old_method,
            app);
}

void hook_call_app_oncreate(JNIEnv *env) {
    jclass in_class=env->FindClass("android/app/Instrumentation");
    //android.app.Application
    jmethodID call_app_oncreate=env->GetMethodID(in_class,"callApplicationOnCreate","(Landroid/app/Application;)V");
    AKJavaHookMethodV(env,call_app_oncreate,
            reinterpret_cast<void*>(my_call_app_oncreate),&old_method);
    LOGD("old_method:%p\n",old_method);
}
